<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:insert="~{include :: header('修改配置信息')}" />
</head>
<body class="hold-transition">
<div class="container-fluid mt-2 mb-2">
    <form class="form-horizontal m" id="form-profile-edit" th:object="${websiteProfile}">
        <input id="modelType"  th:value="${modelType}" type="hidden"/>
        <div class="form-group row">
            <label class="col-sm-3 col-form-label text-sm-right">SSO地址</label>
            <div class="col-sm-6">
                <input id="ssoAddress" name="ssoAddress" class="form-control" th:value="*{get('ssoAddress')}" type="text">
            </div>
        </div>
        <div class="form-group row">
            <label class="col-sm-3 col-form-label text-sm-right"></label>
            <div class="col-sm-9 form-control-static text-red">
                （完整URL，如：https://www.dwvan.com，结尾不能带“/”）
            </div>
        </div>
        <div class="form-group row">
            <label class="col-sm-3 col-form-label text-sm-right">通讯密钥</label>
            <div class="col-sm-6">
                <input id="ssoSecret" maxlength="48" name="ssoSecret" class="form-control" th:value="*{get('ssoSecret')}" type="text">
            </div>
            <div class="col-sm-2">
                <a class="btn btn-warning" onclick="createSecret();">生成</a>
            </div>
        </div>
        <div class="form-group row">
            <label class="col-sm-3"></label>
            <div class="col-sm-offset-2 col-sm-6">
                <button type="button" class="btn btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
                <button type="button" class="btn btn-danger" onclick="window.parent.layer.closeAll()"><i class="fa fa-reply-all"></i>关 闭 </button>
            </div>
        </div>
    </form>
</div>
<th:block th:insert="~{include :: footer}" />
<script type="text/javascript">

    var prefix = "/profile";

    function submitHandler() {
        var modelType = $("#modelType").val();
        if ($.validate.form()) {
            var baseInfo = {};
            var formArray = $("#form-profile-edit").serializeArray();
            $.each(formArray, function (i, item) {
                baseInfo[item.name] = item.value;
            });
            var description = JSON.stringify(baseInfo);
            $.post(prefix + "/save.json", {"type": modelType, "description": description}, function (json) {
                console.log(json)
                if (json.code === 0) {
                    window.parent.layer.closeAll();
                }
            })
        }
    }
    //生成随机密码
    function createSecret() {
        var keyChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        var keyStr = '';
        for (i = 1; i <= 32; i++) {
            if (i % 2 === 0)
                keyStr += Math.floor(Math.random() * 10);
            else
                keyStr += keyChars.charAt(Math.floor(Math.random() * keyChars.length));
        }
        $('#ssoSecret').val(keyStr);
    }
</script>
</body>
</html>